package net.huashitong.synthetical.newSupervision.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.util.SysUserUtils;
import net.huashitong.synthetical.newSupervision.entity.TaskNew;
import net.huashitong.synthetical.newSupervision.entity.UnitPersonnel;
import net.huashitong.synthetical.newSupervision.filter.UnitPersonnelFilter;
import net.huashitong.synthetical.newSupervision.service.UnitPersonnelService;
import net.huashitong.synthetical.student.entity.DemoStudent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/15
 * Time: 11:33
 * To change this template use File | Settings | File Templates.
 * Description:单位人员控制层
 */


@Controller
@RequestMapping("/unitPersonnel")
public class UnitPersonnelController extends JsonBaseController {
    @Autowired
    private UnitPersonnelService unitPersonnelService;
    @Autowired
    private SysDeptService sysDeptService;

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    @Override
    protected String getView(String s) {
        return "/modules/project/unitPersonnel/" + s;
    }

    @RequestMapping("/filter")
    public String filter() {
        return getView("filter");
    }

    /**
     * 查询当前 单位人员
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid queryFilter(UnitPersonnelFilter filter) {
        String deptId = SysUserUtils.getDeptId();
        filter.setDeptId(deptId);
        List<UnitPersonnel> list = unitPersonnelService.queryFilter(filter);
        return coverJqGrid(filter, list);
    }

    @ResponseBody
    @RequestMapping(value = "queryList",method = RequestMethod.POST)
    public List<UnitPersonnel> list(UnitPersonnelFilter filter){
        String deptId = SysUserUtils.getDeptId();
        filter.setDeptId(deptId);
        List<UnitPersonnel> list = unitPersonnelService.queryFilter(filter);
        return list;
    }
    /**
     * 返回新增页面
     *
     * @param model
     * @return url
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(TaskNew taskNew, Model model) {
        return getView("form");
    }

    /**
     * 添加单位人员
     *
     * @param unitPersonnel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute UnitPersonnel unitPersonnel) {
        String result = unitPersonnelService.createUnitPer(unitPersonnel);
        if ("200".equals(result)) {
            return coverMessage("200", "添加成功");
        } else {
            return coverMessage("500", "该用户已存在");
        }
    }

    /**
     * 返回更新页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        UnitPersonnel unitPersonnel = unitPersonnelService.getById(id);
        model.addAttribute("datebean",unitPersonnel);
        return getView("form");
    }

    /**
     * 更新人员信息
     *
     * @param unitPersonnel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Message update(@ModelAttribute UnitPersonnel unitPersonnel) {
        unitPersonnelService.update(unitPersonnel);
        return coverMessage("200");
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/unitPerInfo/{id}",method = RequestMethod.POST)
    public String getById(Model model,@PathVariable String id){
        UnitPersonnel unitPersonnel = unitPersonnelService.getById(id);
        model.addAttribute("unitPersonnel", unitPersonnel);
        return getView("info");
    }

    /**
     * 删除
     * @param ids
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.POST)
    public Message delete(@PathVariable List<String> ids){
        unitPersonnelService.batchDelete(ids);
        return coverMessage("200");
    }


}
